<template>
  <div>
    <div class="mlt" style="margin-bottom: 10px">
      <el-button
        v-permissions="{ permission: ['add:CusSource'] }"
        type="primary"
        @click="addReservationClisk"
      >
        新增来源
      </el-button>
    </div>
    <div class="ml">
      <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
        <el-table-column label="名称" prop="source_name" width="" />
        <el-table-column label="操作" prop="">
          <template slot-scope="scope">
            <el-button
              v-permissions="{ permission: ['edit:CusSource'] }"
              type="primary"
              @click="editSource(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-permissions="{ permission: ['delete:CusSource'] }"
              type="primary"
              @click="details(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :before-close="handleClose"
      title="来源"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" label-width="80px" :model="form">
        <el-form-item label="来源名称">
          <el-input v-model="form.source_name" placeholder="请输入来源名称" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSource">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import { getCusSourceList, addCusSource } from '@/api/gustomerGroup'

  export default {
    // components: {
    //   AddGroup,
    // },
    data() {
      return {
        pagesize: 10,
        emptyText: ' ',
        tableData: [],
        dialogVisible: false,
        form: {
          source_name: '',
        },
      }
    },
    watch: {},
    mounted() {
      this.init()
    },
    methods: {
      init() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        const params = {
          limit: this.pagesize,
        }
        // 用户来源list
        getCusSourceList(params).then((res) => {
          this.tableData = res.data
          loading.close()
          if (res.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
      },
      // 编辑来源
      editSource(e) {
        this.form.source_name = e.source_name
        this.form.id = e.id
        this.dialogVisible = true
      },
      // 添加来源
      addReservationClisk() {
        this.form = {}
        this.dialogVisible = true
      },
      // 提交添加来源
      addSource() {
        const jy = {
          source_name: '请输入来源名称',
        }
        if (Object.keys(this.form).length === 0) {
          this.$message.error('请填写信息')
          return
        } else {
          for (var key in this.form) {
            if (this.form[key] == '') {
              this.$message.error(jy[key])
              return
            }
          }
        }
        addCusSource(this.form).then((res) => {
          if (res.code == 200) {
            this.init()
            this.form = {}
            this.dialogVisible = false
            this.$message({
              message: '成功',
              type: 'success',
            })
          } else {
            this.$message.error('添加失败，请正确操作')
          }
        })
      },
      // 回调弹框
      handleClose() {
        this.$confirm('确认关闭？')
          .then(() => {
            this.form = {}
          })
          .catch(() => {})
      },
      // 详情
      details() {},
    },
  }
</script>
<style></style>
